package test.study.concurrent.thread._3_future.future;

import cn.hutool.core.thread.ThreadUtil;
import com.google.common.base.Stopwatch;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;

@Slf4j
public class _1_Future_Main {

    private static ExecutorService POOL = Executors.newFixedThreadPool(2);

    @SneakyThrows
    public static void main(String[] args) {
        Stopwatch stopwatch = Stopwatch.createStarted();

        //step-1: user thread
        log.info("user thread");
        //step-2: execute
        Future<String> f1 = POOL.submit(() -> {
            log.info("pool thread");
            ThreadUtil.sleep(8, TimeUnit.SECONDS);
            return "123123123";
        });
        Future<String> f2 = POOL.submit(() -> {
            log.info("pool thread");
            ThreadUtil.sleep(6, TimeUnit.SECONDS);
            return "abcabcabc";
        });
        //step-3: get result
        String r1 = f1.get();
        log.info(">>>>>> r1= {}", r1);
        String r2 = f2.get();
        log.info(">>>>>> r2= {}", r2);

        log.info("耗时 {} ms", stopwatch.elapsed(TimeUnit.MILLISECONDS));
        POOL.shutdown();
    }
}
